using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxControlToolkit;
using PietschSoft.VE;

namespace SecurityLinkUp
{
    public partial class Default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // update the text with the most current counts
                CountCommunities.Text = CommunityData.GetCommunityCount(CommonData.Status.ACTIVE).ToString();
                CountChapters.Text = ChapterData.GetChapterCount(CommonData.Status.ACTIVE).ToString();

                // page loading for the first time - populate the communities
                ArrayList Communities = CommunityData.GetCommunities(CommonData.Status.ACTIVE);
                foreach (Community co in Communities)
                {
                    ListItem itm = new ListItem(co.name, co.uid.ToString());
                    CommunityListBox.Items.Add(itm);

                    // get all the active communities for this chapter 
                    ArrayList Chapters = ChapterData.GetChaptersFromCommunityID(co.uid, CommonData.Status.ACTIVE);
                    foreach (Chapter ch in Chapters)
                    {
                        if (ch.HasPosition)
                        {
                            LatLong Position = new LatLong(ch.geolat, ch.geolong);
                            Pushpin p = new PietschSoft.VE.Pushpin(ch.uid.ToString(), Position, null, ch.name, "<a href=/ViewChapter.aspx?ChapterID="+ch.uid+">Go to this chapter</a>");
                            Map.Pushpins.Add(p);
                        }
                    }
                }
            }
        }

        protected void CommunityListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // hide the overview panel
            OverviewPanel.Visible = false;

            // hide the chapter panel
            ChapterPanel.Visible = false;

            // show the community panel
            CommunityPanel.Visible = true;
            
            // update the community info
            CommunitySummary.LoadInfo(new Guid(CommunityListBox.SelectedValue));

            // load the chapters selection box
            ChapterListBox.Items.Clear();
            ArrayList Chapters = ChapterData.GetChaptersFromCommunityID(new Guid(CommunityListBox.SelectedValue), CommonData.Status.ACTIVE);
            foreach (Chapter ch in Chapters)
            {
                ListItem itm = new ListItem(ch.name, ch.uid.ToString());
                ChapterListBox.Items.Add(itm);
            }
        }

        protected void ChapterListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            // hide the community panel
            CommunityPanel.Visible = false;

            // show the chapter panel
            ChapterPanel.Visible = true;

            // load the chapter info
            ChapterSummary.LoadInfo(new Guid(ChapterListBox.SelectedValue));
        }


    } 
}
